qype.API = {
    API_KEY: "NvuKeZPbV92DGlTpiqBOYQ",

    /**
     * Common function to fetch data from the Qype API. Takes care of setting the
     * needed HTTP headers etc.
     *
     * Examples:
     *      qype.API.get("places", "42")
     *      qype.API.get("places", "42",
     *                   { "foo": 1 },
     *                   {
     *                     "onSuccess": function() { alert("success!"); },
     *                     "onFailure": function() { alert("failure!"); }
     *                   })
     * 
     * @param {String} target The target Qype API command (e.g. "places")
     * @param {String} path Path to be appended to the base url (e.g. "42") (optional)
     * @param {Object} params Parameters to be appended to the url (optional)
     * @param {Object} callbacks Callbacks, should contain at least
     *                 onSuccess and onFailure callbacks. (optional)
     */
    get: function(target, callbacks, path, params) {
        var url = "http://api.qype.com/v1/" + target + (path ? ("/"+path) : "");

        params = params || {};
        params.consumer_key = this.API_KEY

        callbacks = callbacks || this.defaultCallbacks;

        new Ajax.Request(url, {
            method: "get",
            parameters: params,
            requestHeaders: {
               "Accept": "application/json",
               "Accept-Language": "en_GB"
            },
            onSuccess: function(response) { 
				if(!response.status) { 
					callbacks.onFailure(response);
				} else {
					callbacks.onSuccess(response);
				}
			},
            onFailure: callbacks.onFailure
        });
    },

    defaultCallbacks: {
        onSuccess: function(response) {
            util.log("{Qype.API} onSuccess:");
            util.log(response);
        },

        onFailure: function(response) {
            util.log("{Qype.API} onFailure:");
            util.log(response);
        }
    }
};